#!/bin/sh
#package:odpkg
#desc:Add package repository
#type:local
#
#TODO Verificar la calidad del repositorio (firmas, hashes, certs, etc.)

# Copyright(c) 2011 OpenDomo Services SL. Licensed under GPL v3 or later


CONFIGFILE='/etc/opendomo/repositories.cfg'
TMPFILE='/tmp/uhltmp'

if test -f "$CONFIGFILE"; then
	echo "#> Configured repositories"
	echo "list:`basename $0`	selectable"
	for i in `cat $CONFIGFILE`; do
		CID=`echo $i | sed -e 's/http\:\/\///g' -e 's/\/$//' -e 's/[^a-z0-9]/_/g'`
		echo "	-$CID	$i	$i"
	done
	echo "actions:"
	echo "	delPackageRepository.sh	Delete package repository"
	echo 
fi
	
if test -n "$1"; then
# Se ha pasado un nuevo repositorio como parametro
	DESC=`wget -q -O - "$1" | grep "#desc" | cut -f2- -d:`
	if test -n "$DESC"; then		
		cat $CONFIGFILE > $TMPFILE
		echo $1 >> $TMPFILE
		uniq $TMPFILE > $CONFIGFILE
		rm $TMPFILE
		chown admin $CONFIGFILE
		echo "# Repository [$DESC] added"
		. /usr/local/opendomo/updatePackageList.sh 
	else
		echo "#ERR: Invalid repository [$1]"
		exit 1
	fi
fi

echo "#> Add new repository"
echo "usage:`basename $0`"
echo "	url	URL	text.url	http://www.opendomo.org/files/pkg/"
echo "actions:"
echo "	addPackageRepository.sh	Add package repository"
echo 
